﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Masters/Main.Master" AutoEventWireup="true"
  CodeBehind="Default.aspx.cs" Inherits="elmah2.Viewer.Default" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
  <link href="/css/flexigrid.css" rel="stylesheet" type="text/css" />
  <script src="/scripts/flexigrid.pack.js" type="text/javascript"></script>
  
  <script type="text/javascript">
    var parms;

    $(document).ready(function()
    {
      parms = new Array();
      parms[0] = { name: "app", value: "" };

      //load loggers
      $.ajax(
        {
          type: "POST",
          dataType: "json",
          url: "/Data.asmx/GetLoggers",
          contentType: "application/json; charset=utf-8",
          async: false,
          error: AjaxError,
          success: function(data)
          {
            if (data.length == 0)
            {
              alert("No loggers found");
            }
            else
            {
              for (var i = 0; i < data.d.length; i++)
              {
                var name = data.d[i];
                $('#loggers').append("<option id='" + name + "'>" + name + "</option>");
              }
            }
          }
        });

      $('#loggers').change(LoadApplications);
      $('#apps').change(ReloadErrors);
      LoadApplications();

      $("#errorGrid").flexigrid({
        url: 'GetErrors.ashx',
        dataType: 'json',
        colModel: [
				{ display: 'Host', name: 'Host', width: 75, sortable: false, align: 'left' },
				{ display: 'App', name: 'App', width: 50, sortable: false, align: 'left' },
				{ display: 'Code', name: 'Code', width: 50, sortable: false, align: 'left' },
				{ display: 'Error', name: 'Error', width: 210, sortable: false, align: 'left' },
				{ display: 'Type', name: 'Type', width: 90, sortable: false, align: 'left' },
				{ display: 'User', name: 'User', width: 90, sortable: false, align: 'left' },
				{ display: 'Date', name: 'Date', width: 80, sortable: false, align: 'left' },
				{ display: 'Time', name: 'Time', width: 80, sortable: false, align: 'left' },
				],
        usepager: true,
        singleSelect: true,
        title: 'Errors',
        width: 800,
        height: 400,
        useRp: true,
        rp: 15,
        params: parms
      });
    });

    function ReloadErrors()
    {
      var logger = $('#loggers').val();
      parms[0] = { name: "app", value: $('#apps').val() };
      $("#errorGrid").flexOptions({ params: parms });
      $("#errorGrid").flexReload();      
    }
    
    function LoadApplications()
    {
      var logger = $('#loggers').val();
      parms[1] = { name: "logger", value: logger };
      $("#flex1").flexOptions({params: parms});

      $('#apps').children().remove()
      
      $.ajax(
        {
          type: "POST",
          dataType: "json",
          data: "{\"logger\" : \"" + logger + "\"}",
          url: "/Data.asmx/GetApplications",
          contentType: "application/json; charset=utf-8",
          async: false,
          error: AjaxError,
          success: function(data)
          {
            $('#apps').append("<option value='-1'>All</option>");
            for (var i = 0; i < data.d.length; i++)
            {
              var name = data.d[i].Name;
              var ID = data.d[i].ID;
              
              $('#apps').append("<option value='" + ID + "'>" + name + "</option>");
            }
          }
      });
      ReloadErrors();
    }

    function AjaxError(error) 
    {
      //TODO Notify User of err
    }
  </script>

</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="pageTitle" runat="server">
  Error Log
</asp:Content>

<asp:Content ID="Content3" ContentPlaceHolderID="mainContent" runat="server">
  <br />
  Loggers:
  <select id="loggers">
  </select>
  Application:
  <select id="apps">
    <option value="-1">All</option>
  </select>
  
  <table id="errorGrid"></table>
</asp:Content>
